import { _list } from '../../common/api/guest'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    keyword: '',
    currentIndex: 0,
    tabs: ['跟进', '来访','认筹','认购','签约'],
    lists: [],
    pageIndexes: [],
    pageSize: 5,
    totalCount: [],
    curTotalCount: []
  },
  computed: {
    stateType () {
      return this.data.tabs[this.data.currentIndex]
    },
    pageIndex () {
      return this.data.pageIndexes[this.data.currentIndex]
    },
    finished () {
      return this.data.curTotalCount.map((item, index) => {
        return this.data.lists.length ? this.data.lists[index].length >= item : false
      })
    }
  },
  methods: {
    inputHandler (e) {
      console.log(e)
      this.setData({
        keyword: e.detail.value
      })
    },
    initDatas () {
      this.data.pageIndexes = []
      this.data.totalCount = []
      this.data.curTotalCount = []
      for (let i = 0; i < this.data.tabs.length; i++) {
        this.data.pageIndexes.push(1)
        this.data.totalCount.push(0)
        this.data.curTotalCount.push(0)
      }
      this.setData({
        pageIndexes: this.data.pageIndexes,
        totalCount: this.data.totalCount,
        curTotalCount: this.data.curTotalCount
      })
    },
    tabChange(e) {
      let currentIndex = parseInt(e.detail)
      this.setData({
        currentIndex
      })
    },
    swiperChange(e) {
      let currentIndex = parseInt(e.detail.current)
      this.setData({
        currentIndex
      })
    },
    totalQuery() {
      console.log(this.data.keyword)
      let uid = wx.getStorageSync('uid')
      app.loading('加载中')
      let queryArr = []
      for (let i = 0; i < this.data.tabs.length; i++) {
        queryArr.push(
          Promise.resolve(
            _list({
              UnionID: uid,
              KeyWord: this.data.keyword,
              StateType: this.data.tabs[i],
              PageIndex: this.data.pageIndexes[i],
              PageSize: this.data.pageSize
            })
          )
        )
      }
      Promise.all(queryArr).then(res => {
        wx.hideLoading()
        let lists = []
        let totalCount = []
        let curTotalCount = []
        for (let i = 0; i < res.length; i++) {
          let arr = res[i].data.Data.CustomerList
          lists.push(arr)
          totalCount.push(res[i].data.Data.TotalCount)
          curTotalCount.push(res[i].data.Data.DataCount)
        }
        this.setData({
          lists,
          totalCount,
          curTotalCount
        })
        wx.stopPullDownRefresh()
      }).catch(err => {
        wx.stopPullDownRefresh()
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err) || '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    concatList() {
      let uid = wx.getStorageSync('uid')
      let currentIndex = this.data.currentIndex
      let pageIndex = this.data.pageIndex
      _list({
        UnionID: uid,
        KeyWord: this.data.keyword,
        StateType: this.data.stateType,
        PageIndex: pageIndex,
        PageSize: this.data.pageSize
      }).then(res => {
        let list = res.data.Data.CustomerList
        this.data.lists[currentIndex] = this.data.lists[currentIndex].concat(list)
        let str = `lists[${currentIndex}]`
        this.setData({
          [str]: this.data.lists[currentIndex]
        })
      }).catch(err => {
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err) || '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    search () {
      this.initDatas()
      this.totalQuery()
    },
    onReachLower() {
      let currentIndex = this.data.currentIndex
      if (this.data.finished[currentIndex]) {
        return
      }
      this.data.pageIndexes[currentIndex] += 1
      let pageindex = `pageIndexes[${currentIndex}]`
      this.setData({
        [pageindex]: this.data.pageIndexes[currentIndex]
      }, () => {
        this.concatList()
      })
    },
    onLoad(options) {},
    onReady() { },
    onShow() {
      this.initDatas()
      app.launch().then(res => {
        this.setData({
          userinfo: res
        })
        this.totalQuery()
      }).catch(err => {
        console.log(err)
        if (err === '用户未注册') {
          wx.showModal({
            title: '对不起',
            content: '您还未注册',
            showCancel: false,
            success: r => {
              if (r.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      })
    },
    onHide() { },
    onUnload() { },
    onPullDownRefresh() {
      this.initDatas()
      this.totalQuery()
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})